package es.abredatos.client;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HasWidgets;

import es.abredatos.client.event.GetBookDetailsEvent;
import es.abredatos.client.event.GetBookDetailsEventHandler;
import es.abredatos.client.event.GoHomeEvent;
import es.abredatos.client.event.GoHomeEventHandler;
import es.abredatos.client.event.LocateLibraryEvent;
import es.abredatos.client.event.LocateLibraryEventHandler;
import es.abredatos.client.event.SearchBookEvent;
import es.abredatos.client.event.SearchBookEventHandler;
import es.abredatos.client.presenter.BookPresenter;
import es.abredatos.client.presenter.BooksPresenter;
import es.abredatos.client.presenter.HomePresenter;
import es.abredatos.client.presenter.LibrariesPresenter;
import es.abredatos.client.presenter.Presenter;
import es.abredatos.client.view.BookView;
import es.abredatos.client.view.BooksView;
import es.abredatos.client.view.HomeView;
import es.abredatos.client.view.LibrariesView;

public class AppController implements ValueChangeHandler<String> {

	private BooksLocatorServiceAsync rpcService;
	private HandlerManager eventBus;
	private HasWidgets container;

	public AppController(BooksLocatorServiceAsync rpcService, HandlerManager eventBus) {
		this.rpcService = rpcService;
		this.eventBus = eventBus;

		bind();
	}

	public void go(HasWidgets container) {
		this.container = container;

		eventBus.fireEvent(new GoHomeEvent());
	}

	private void bind() {
		History.addValueChangeHandler(this);

		eventBus.addHandler(GoHomeEvent.TYPE, new GoHomeEventHandler() {
			public void onGoHomeEvent(GoHomeEvent event) {
				doGoHome();
			}
		});

		eventBus.addHandler(SearchBookEvent.TYPE, new SearchBookEventHandler() {
			public void onSearchBook(SearchBookEvent event) {
				doSearchBook(event.getTitle());
			}
		});

		eventBus.addHandler(GetBookDetailsEvent.TYPE, new GetBookDetailsEventHandler() {
			public void onGetBookDetailsEvent(GetBookDetailsEvent event) {
				doGetBookDetails(event.getId());
			}
		});

		eventBus.addHandler(LocateLibraryEvent.TYPE, new LocateLibraryEventHandler() {
			public void onLocateLibraryEvent(LocateLibraryEvent event) {
				doLocateLibrary(event.getId());
			}
		});
	}

	public void onValueChange(ValueChangeEvent<String> event) {
		String token = event.getValue();

		if (token.equals(HomePresenter.class.getName())) {
		} else if (token.equals(BookPresenter.class.getName())) {
		} else if (token.equals(BookPresenter.class.getName())) {
		} else if (token.equals(LibrariesPresenter.class.getName())) {
		}

		System.out.println("onValueChange " + token);

	}

	private void doGoHome() {
		History.newItem(HomePresenter.class.getName());

		Presenter presenter = new HomePresenter(rpcService, eventBus, new HomeView());
		presenter.go(container);
	}

	private void doSearchBook(String title) {
		History.newItem(BooksPresenter.class.getName());

		Presenter presenter = new BooksPresenter(rpcService, eventBus, new BooksView(), title);
		presenter.go(container);
	}

	private void doGetBookDetails(String id) {
		History.newItem(BookPresenter.class.getName());

		Presenter presenter = new BookPresenter(rpcService, eventBus, new BookView(), id);
		presenter.go(container);
	}

	private void doLocateLibrary(String id) {
		History.newItem(LibrariesPresenter.class.getName());

		Presenter presenter = new LibrariesPresenter(rpcService, eventBus, new LibrariesView(), id);
		presenter.go(container);
	}

}
